﻿﻿@using FCZLM.Core.Models
@{
    Layout = null;
    var list = ViewBag._productCategorysRepository as List<ProductCategorys>;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <link href="/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <div>
        <div class="layui-card">
            <div class="layui-card-header">添加产品</div>
            <div class="layui-card-body">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="Name" required lay-verify="required" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="Price" required lay-verify="required" placeholder="请输入产品金额" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">品牌</label>
                        <div class="layui-input-block">
                            <input type="text" name="Brand" required lay-verify="required" placeholder="请输入品牌名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品描述</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" id="content" placeholder="请输入内容" style="display: none" name="Desc" autocomplete="off"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品分类</label>
                        <div class="layui-input-block">
                            <select name="ProductCategoryId" lay-verify="required">
                                @foreach (var item in list)
                                {
                                    <option value="@item.Id">@item.Name</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="Status" value="1" title="上架" checked>
                            <input type="radio" name="Status" value="0" title="下架">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="Addform" data-type="text" id="fileNotifySubmit">添加</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <script src="/layui/layui.js"></script>
        <script>
            layui.use(['layedit', 'form'], function () {
                var layedit = layui.layedit
                    , $ = layui.jquery
                    , form = layui.form;
                //设置图片上传
                layedit.set({
                    uploadImage: {
                        url: '/fproducturls/index' //图片上传方法的接口url
                        , type: 'post' //默认post
                    }
                });
                //构建富文本编辑器，构建方法一定要写在set方法下边
                var index = layedit.build('content');

                //校验表单的方法，并将富文本编辑器中的内容同步到文本域中
                form.verify({
                    content: function (value) {
                        return layedit.sync(index); //同步语句
                    }
                });

                //保存表单内容提交的方法
                $('#fileNotifySubmit').on('click', function () {
                    var active = {
                        //获取纯文本内容： 111
                        //如果要获取样式，将text替换成 content: <p>111</p>
                        //如果要获取选中的文字，将text替换成selected：111
                        text: function () {
                            //获取富文本编辑器值的语句并赋值到文本域中
                            document.getElementById("content").innerText = layedit
                                .getContent(index);
                        }
                    };

                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                    var form = new FormData(document.getElementById("formFile"));

                    //保存表单内容的请求，此处为文件通知对象
                    $.ajax({
                        url: "/fproducts/add",
                        type: "post",
                        data: form,
                        processData: false,
                        contentType: false,
                        success: function (result) {
                            if (result.status === true) {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.window.location.reload()
                            } else {
                                layer.msg(result.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            });
            //重新加载的方法
            layui.use(['table', 'laydate', 'element'], function () {
                var table = layui.table, form = layui.form, $ = layui.jquery;
            });
            layui.use(["form", 'layer'], function () {
                var form = layui.form;
                var $ = layui.$;
                var layer = layui.layer;
                //提交
                form.on('submit(Addform)', function (data) {
                    $.post("/fproducts/add", data.field, function (res) {
                        if (res.isSuccess) {
                            layer.msg(res.msg, function () {
                                parent.window.location.href = "/fproducts/index";
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                    return false;
                });
            });
        </script>
    </div>
</body>
</html>
